<template>
	<view class="container">
		<van-search :value="value" placeholder="输入城市名" show-action @search="onSearch" @cancel="onCancel" @change="onChange" />
		<view style="position: fixed; z-index: 999; width: 100%">
			<van-cell :title="item.name" v-for="(item, index) in searchdata" :key="index"></van-cell>
		</view>
		<van-index-bar highlight-color="#ee0a24">
			<view v-for="(region, index) in regionList" :key="index">
				<van-index-anchor :index="region.name" />
				<van-cell :title="ritem.name" @tap="setzone(ritem.id)" v-for="(ritem,index1) in region.list" :key="index1" />
			</view>
		</van-index-bar>
	</view>
</template>

<script>
	import { getPage } from '../../utils/config';
	import api from '../../utils/api';
	import { http, $empty, $toast, storage, Validate } from '../../utils/utils';
	import wxApi from '../../utils/wxApi';
	const app = getApp();
	export default {
	    data() {
	        return {
	            allList: [],
	            regionList: [],
	            value: '',
	            searchdata: [],
	            getPages: '',
	
	            region: {
	                name: '',
	                list: []
	            },
	
	            ritem: {
	                name: '',
	                id: ''
	            }
	        };
	    }
	    /**
	     * 生命周期函数--监听页面加载
	     */,
	    onLoad(options) {
	        //获取路由数量
			this.getPages = getCurrentPages().length
	        this.getRegionInfo();
	    },
	    /**
	     * 生命周期函数--监听页面初次渲染完成
	     */
	    onReady() {},
	    /**
	     * 生命周期函数--监听页面显示
	     */
	    onShow() {},
	    /**
	     * 生命周期函数--监听页面隐藏
	     */
	    onHide() {},
	    /**
	     * 生命周期函数--监听页面卸载
	     */
	    onUnload() {},
	    /**
	     * 页面相关事件处理函数--监听用户下拉动作
	     */
	    onPullDownRefresh() {},
	    /**
	     * 页面上拉触底事件的处理函数
	     */
	    onReachBottom() {},
	    /**
	     * 用户点击右上角分享
	     */
	    onShareAppMessage() {},
	    methods: {
	        onChange(e) {
	            var regionList = this.regionList;
	            var allList = [];
	            var searchdata = [];
	            if (this.allList.length == 0) {
	                regionList.forEach((item) => {
	                    item.list.forEach((item1) => {
	                        allList.push(item1);
	                    });
	                });
					this.allList = allList;
	            } else {
	                allList = this.allList;
	            }
	            allList.forEach((item) => {
	                if (e.detail != '') {
	                    if (item.name.indexOf(e.detail) > -1) {
	                        searchdata.push(item);
	                    }
	                }
	            });
	            if (e.detail == '') {
	                searchdata = [];
	            }
				this.value = e.detail;
				this.allList = e.allList;
				this.searchdata = e.searchdata;
	        },
	
	        onSearch() {
	            $toast('搜索' + this.value);
	        },
	
	        /**
	         * 获取地区
	         */
	        getRegionInfo() {
	            return new Promise((resolve) => {
	                storage
	                    .get('regionList')
	                    .then((res) => {
							this.regionList = res
	                        resolve(res);
	                    })
	                    .catch((res) => {
	                        console.log('2323432');
	                        http.get(
	                            api.default,
	                            {
	                                data: {
	                                    action: 'getlocation'
	                                }
	                            },
	                            false
	                        ).then((res) => {
	                            const { data } = res.data;
								this.regionList = res
	                            storage.set('regionList', data);
	                            resolve(data);
	                        });
	                    });
	            });
	        },
	
	        setzone(id) {
	            app.globalData.zoneid = id;
	            //获取设置城市id
	            http.post(
	                api.default,
	                {
	                    data: {
	                        action: 'getregioninfo',
	                        id: app.globalData.zoneid
	                    }
	                },
	                false
	            ).then((res) => {
	                const { data } = res.data;
	                if (data != null) {
	                    app.globalData.zone = data;
	                    //存在上一页,关闭当前返回一层,//没有上一页，跳回首页
	                    wxApi.refreshPage(getPage.home);
	                }
	            });
	        },
	
	        onCancel() {
	            console.log('占位：函数 onCancel 未声明');
	        }
	    }
	};
</script>

<style>
@import './location.css';
</style>
